//npm install -g cnpm --registry=https://registry.npm.taobao.org

//npm install gulp -g;
//npm install --save-dev gulp-sass;
//npm install --save-dev gulp-minify-css;
//npm install --save-dev gulp-autoprefixer;
//npm install --save-dev gulp-useref;
//npm install --save-dev gulp-cachebust;
//npm install --save-dev gulp-uglify;
//npm install --save-dev gulp-strip-debug;
//npm install --save-dev gulp-imagemin;
//npm install --save-dev gulp-clean;
//npm install --save-dev gulp-if;
//npm i gulp-htmlmin --save-dev;
//npm install imagemin-pngquant --save-dev;
//npm install run-sequence --save-dev;

// 载入外挂

var gulp = require('gulp');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var autoprefixer = require('gulp-autoprefixer');

var uglify = require('gulp-uglify'); // 压缩
var stripDebug = require('gulp-strip-debug'); // 该插件用来去掉console和debugger语句
var useref = require('gulp-useref');
const imagemin = require('gulp-imagemin');

var clean = require('gulp-clean');
var gulpif = require('gulp-if');

var htmlmin = require('gulp-htmlmin');

var CacheBuster = require('gulp-cachebust');

const runSequence = require('run-sequence')


var cachebust = new CacheBuster();

var pngquant = require('imagemin-pngquant');

var browserSync = require('browser-sync').create();
var reload = browserSync.reload;

var gutil = require('gulp-util');

// 任务处理的文件路径配置
var paths = {
    html: [
        '**/*.html'
    ],
    js: [
        'js/*.js',
        'js/base/**/*.*',
        'js/theme/**/*.*'
    ],
    sass: [
        'sass/*.scss'
    ],
    css: [
        'css/**/*.css'
    ],
    css_cache: [
        'css_cache/**/*.css'
    ],
    img: [
        'img/**/*'
    ],
    test: [
        'test/**/*.*'
    ],
    html_path: [
        ''
    ],
    js_path: [
        'js/',
        'js/base/',
        'js/theme/'
    ],
    sass_path: [
        'sass/'
    ],
    css_path: [
        'css/'
    ],
    css_cache_path: [
        'css_cache/'
    ],
    img_path: [
        'img/'
    ],
    test_path: [
        'test/'
    ],
    source_path: [
        'src/'
    ],
    output_path: [
        'dist/'
    ]
};

gulp.task('clean', function(cb) {
    var stream = gulp.src(paths.output_path[0])
        .pipe(clean({
            force: true
        }));

    return stream;
});

// Copy vendor libraries from /bower_components into /vendor
gulp.task('copy', function() {
    gulp.src(['bower_components/bootstrap/dist/**/*', '!**/npm.js', '!**/bootstrap-theme.*', '!**/*.map'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/bootstrap'))

    gulp.src(['bower_components/bootstrap-social/*.css', 'bower_components/bootstrap-social/*.less', 'bower_components/bootstrap-social/*.scss'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/bootstrap-social'))

    gulp.src(['bower_components/datatables/media/**/*'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/datatables'))

    gulp.src(['bower_components/datatables-plugins/integration/bootstrap/3/*'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/datatables-plugins'))

    gulp.src(['bower_components/datatables-responsive/css/*', 'bower_components/datatables-responsive/js/*'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/datatables-responsive'))

    gulp.src(['bower_components/flot/*.js'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/flot'))

    gulp.src(['bower_components/flot.tooltip/js/*.js'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/flot-tooltip'))

    gulp.src(['bower_components/font-awesome/**/*', '!bower_components/font-awesome/*.json', '!bower_components/font-awesome/.*'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/font-awesome'))

    gulp.src(['bower_components/jquery/dist/jquery.js', 'bower_components/jquery/dist/jquery.min.js'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/jquery'))

    gulp.src(['bower_components/metisMenu/dist/*'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/metisMenu'))

    gulp.src(['bower_components/morrisjs/*.js', 'bower_components/morrisjs/*.css', '!bower_components/morrisjs/Gruntfile.js'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/morrisjs'))

    gulp.src(['bower_components/raphael/raphael.js', 'bower_components/raphael/raphael.min.js'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/raphael'))

    gulp.src(['bower_components/vue/dist/vue.js', 'bower_components/vue/dist/vue.min.js'])
        .pipe(gulp.dest(paths.output_path[0] + 'vendor/vue'))


    gulp.src(['src/data/*.*'])
        .pipe(gulp.dest(paths.output_path[0] + 'data'))

})

gulp.task('build-test', function() {
    return gulp.src(paths.source_path[0] + paths.test[0])
        .pipe(gulp.dest(paths.output_path[0] + paths.test_path[0]))
        .pipe(browserSync.stream({
            once: true
        }));
});

// gulp.task('autoprefixer', ['sass'], function () {
//     return gulp.src(paths.css)
//         .pipe(autoprefixer({
//             cascade: false
//         }))
//         .pipe(gulp.dest(paths.output_path[0] + paths.css_cache_path[0]));
// })

// gulp.task('build-css', ['autoprefixer'], function () {
// return gulp.src(paths.output_path[0] + paths.css_cache[0])
// Awesome css stuff 

// gulp.task('build-css', ['clean'], function () {
//     return gulp.src(paths.sass)
//         .pipe(sass())
//         .pipe(autoprefixer({
//             cascade: false
//         }))
//         .pipe(cachebust.resources())
//         .pipe(gulpif('*.css', minifyCss()))
//         .pipe(gulp.dest(paths.output_path[0] + paths.css_path[0]));
// })

// gulp.task('build-html', ['clean', 'building-img', 'build-css', 'build-js', 'build-js-base', 'build-test'], function ()
gulp.task('build-html', function(done) {
    return gulp.src(paths.source_path[0] + paths.html[0])
        // Awesome html stuff 
        .pipe(cachebust.references())
        .pipe(htmlmin({
            removeComments: true
        }))
        .pipe(gulp.dest(paths.output_path[0] + paths.html_path[0]))
});

gulp.task('build-css', function(done) {
    var folderPath = paths.output_path[0] + paths.css_path[0];

    return gulp.src(paths.source_path[0] + paths.sass[0])
        .pipe(sass())
        .pipe(autoprefixer({
            cascade: false
        }))
        .pipe(cachebust.resources())
        // .pipe(gulpif('*.css', minifyCss()))
        .pipe(gulp.dest(folderPath))
})

gulp.task('build-pub-css', function(done) {
    var folderPath = paths.output_path[0] + paths.css_path[0];

    return gulp.src(paths.source_path[0] + paths.sass[0])
        .pipe(sass())
        .pipe(autoprefixer({
            cascade: false
        }))
        .pipe(cachebust.resources())
        .pipe(gulpif('*.css', minifyCss()))
        .pipe(gulp.dest(folderPath))
})

gulp.task('build-pub-js', function(done) {
    var folderPath = paths.output_path[0] + paths.js_path[0];

    return gulp.src(paths.source_path[0] + paths.js[0])
        .pipe(uglify())
        .pipe(cachebust.resources())
        .pipe(gulp.dest(folderPath))
})
gulp.task('build-js', function(done) {
    var folderPath = paths.output_path[0] + paths.js_path[0];

    return gulp.src(paths.source_path[0] + paths.js[0])
        .pipe(cachebust.resources())
        .pipe(gulp.dest(folderPath))
})

gulp.task('build-js-base', function() {
    return gulp.src(paths.source_path[0] + paths.js[1])
        // .pipe(uglify())
        .pipe(gulp.dest(paths.output_path[0] + paths.js_path[1]))
        .pipe(browserSync.stream({
            once: true
        }));
})

gulp.task('build-js-theme', function() {
    return gulp.src(paths.source_path[0] + paths.js[2])
        // .pipe(uglify())
        .pipe(gulp.dest(paths.output_path[0] + paths.js_path[2]))
        .pipe(browserSync.stream({
            once: true
        }));
})

gulp.task('build-img', function(done) {
    return gulp.src(paths.source_path[0] + paths.img[0])
        .pipe(imagemin({
            progressive: true,
            svgoPlugins: [{
                removeViewBox: false
            }], //不要移除svg的viewbox属性
            use: [pngquant()] //使用pngquant深度压缩png图片的imagemin插件
        }))
        .pipe(gulp.dest(paths.output_path[0] + paths.img_path[0]))
        .pipe(browserSync.stream({
            once: true
        }))

});

gulp.task('watch', function() {

    // gulp.watch(paths.source_path[0] + paths.sass[0], ['build-css', 'build-html']).on('change', reload);
    gulp.watch(paths.source_path[0] + paths.sass[0], function() {
        runSequence(
            'build-css',
            'build-html'
        );
    }).on('change', reload);
    gulp.watch(paths.source_path[0] + paths.html[0], ['build-html']).on('change', reload);
    // gulp.watch(paths.source_path[0] + paths.js[0], ['build-js', 'build-html']).on('change', reload);
    gulp.watch(paths.source_path[0] + paths.js[0], function() {
        runSequence(
            'build-js',
            'build-html'
        );
    }).on('change', reload);
    gulp.watch(paths.source_path[0] + paths.test[0], ['build-test']);
    gulp.watch(paths.source_path[0] + paths.img[0], ['building-img']);

    browserSync.init({
        server: {
            baseDir: './dist'
        },
        //显示了我对过程的其他信息 
        // logLevel: "debug",

        //只是显示基本信息 
        // logLevel: "info",

        //等待任何浏览器应该尝试注入/加载的文件。 (时间)
        reloadDelay: 2000
    });
});

gulp.task('pub', function() {
    runSequence(
        'clean', ['build-pub-css', 'build-pub-js'], ['build-html', 'build-img', 'build-js-base', 'copy'] 
        //['build-js-theme']
    );
    // gulp.start('build-html', 'building-img', 'build-css', 'build-js', 'build-js-base', 'copy');
});

gulp.task('default', function() {
    runSequence(
        'clean',
        // ['build-html', 'build-img', 'build-css', 'build-js-base', 'copy'],
        ['build-css', 'build-js'], ['build-html', 'build-img', 'build-js-base', 'copy', 'build-test'], 
        //['build-js-theme'],
        'watch'
    );
    // gulp.start('build-html', 'watch');
});